#include "DirOp.h"

DirOp::DirOp(QObject *parent) : QObject(parent)
{

}

bool copyDir(const QString &source, const QString &destination, bool override)
{
    QDir directory(source);
    if (!directory.exists())
    {
        return false;
    }

    QString srcPath = QDir::toNativeSeparators(source);
    if (!srcPath.endsWith(QDir::separator()))
        srcPath += QDir::separator();
    QString dstPath = QDir::toNativeSeparators(destination);
    if (!dstPath.endsWith(QDir::separator()))
        dstPath += QDir::separator();

    bool error = false;
    QStringList fileNames = directory.entryList(QDir::AllEntries | QDir::NoDotAndDotDot | QDir::Hidden);

    for (QStringList::size_type i=0; i != fileNames.size(); ++i)
    {
        QString fileName = fileNames.at(i);
        QString srcFilePath = srcPath + fileName;
        QString dstFilePath = dstPath + fileName;
        QFileInfo fileInfo(srcFilePath);
        if (fileInfo.isFile() || fileInfo.isSymLink()) {
            if (override){
                QFile::setPermissions(dstFilePath, QFile::WriteOwner);
            }
            if(QFile::copy(srcFilePath, dstFilePath)){
                directory.remove(srcFilePath);
            }

        }
        else if (fileInfo.isDir()) {
            QDir dstDir(dstFilePath);
            if(dstDir.mkpath(dstFilePath)){
                directory.rmdir(srcFilePath);
            }
            if (!copyDir(srcFilePath, dstFilePath, override)){
                error = true;
            }
        }
    }

    return !error;
}


bool DirOp::creatDir(QString Path, QString DirName)
{
    QDir *folder = new QDir();
    QString CurrentPath = Path +"/" + DirName;
    bool exist = folder->exists(CurrentPath);
    if(exist){
        return true;
    }
    else
    {
        bool result = folder->mkpath(CurrentPath);
        return result;
    }
}

bool DirOp::creatFile(QString Path, QString DirName, QString format)
{
    QString filePath = Path + "/" + DirName + format;
    QFile file(filePath);
    if(file.exists())
    {
        return true;
    }else
    {
       return false;
    }
}

bool DirOp::writeFile(QString filePath, QString content)
{
    QFile file(filePath);
    QByteArray str = content.toUtf8();
    if(file.exists())
    {
        file.write(str);
    }else
    {
        if(!file.open(QIODevice::ReadWrite | QIODevice::Text)){
            return false;
        }
    }
    file.close();
    return true;
}

bool DirOp::getDirIsExist(QString Path)
{
    QDir folder(Path);
    return folder.exists();
}

bool DirOp::getFileIsExist(QString fileName)
{
    QFile file(fileName);
    return file.exists();
}

bool DirOp::deleteDir(const QString &dirName)
{
    QDir directory(dirName);
    if (!directory.exists())
    {
        return true;
    }
    return directory.removeRecursively();
}

bool DirOp::moveDir2Dir(QString source, QString target)
{
    QDir directory(source);
    if (!directory.exists())
    {
        qDebug()<<__FUNCTION__<<"目标路径不存在!";
        return false;
    }
    QString targetName=source.section('/', -1);
    bool ret = false;


    QDir dstDir(target);
    if(dstDir.mkpath(targetName)){
        ret = copyDir(source,target+"/"+targetName,true);
        directory.removeRecursively();
    }
    return  !ret;
}

DirOp * DirOp::instance = nullptr;

DirOp *DirOp::GetInstance()
{
    if(nullptr == instance)
    {
        instance = new DirOp();
    }
    return instance;
}
